package top.anka1.fxgui;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
import top.anka1.ims.ZooGUI;
import top.anka1.ims.ZooManagementSystem.*;

import java.util.ArrayList;
/**
 * This class is responsible for handling the Find Animal functionality in the Zoo Management System.
 * It allows users to search for animals by name and displays the results in a table.
 * @author Anka
 */
public class FindAnimalController {
    private Stage stage;
    private ZooGUI zooGUI;
    @FXML
    private TextField nameField;
    @FXML
    private Label findAnimalLabel;
    @FXML
    private TableView<Animal> animalTable;
    @FXML
    private TableColumn<Animal, String> nameColumn;
    @FXML
    private TableColumn<Animal, String> speciesColumn;
    @FXML
    private TableColumn<Animal, Integer> ageColumn;

    public void setData(Stage stage, ZooGUI zooGUI) {
        this.stage = stage;
        this.zooGUI = zooGUI;
    }
    @FXML
    public void findAnimal() {
        String name = nameField.getText();
        ArrayList<Animal> SouthernZoneAnimals = zooGUI.getAnimalList("Southern-Zone Zoo");
        ArrayList<Animal> NorthernZoneAnimals = zooGUI.getAnimalList("Northern-Zone Zoo");
        nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
        nameColumn.setStyle("-fx-alignment: CENTER;");
        speciesColumn.setCellValueFactory(new PropertyValueFactory<>("species"));
        speciesColumn.setStyle("-fx-alignment: CENTER;");
        ageColumn.setCellValueFactory(new PropertyValueFactory<>("age"));
        ageColumn.setStyle("-fx-alignment: CENTER;");
        for (Animal animal : SouthernZoneAnimals) {
            if (animal.getName().equalsIgnoreCase(name)) {
                findAnimalLabel.setText("Animal found in Southern-Zone Zoo: " + animal.getName());
                ObservableList<Animal> animals = FXCollections.observableArrayList();
                animals.add(animal);
                animalTable.setItems(animals);
                return;
            }
        }
        for (Animal animal : NorthernZoneAnimals) {
            if (animal.getName().equalsIgnoreCase(name)) {
                findAnimalLabel.setText("Animal found in Northern-Zone Zoo: " + animal.getName());
                ObservableList<Animal> animals = FXCollections.observableArrayList();
                animals.add(animal);
                animalTable.setItems(animals);
                return;
            }
        }
        findAnimalLabel.setText("Animal not found");
        animalTable.setItems(FXCollections.observableArrayList());
    }
}
